package com.example.sunjian.myapplication.scroller;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.LinearLayout;
import android.widget.Scroller;

/**
 * 作者：sunjian on 16/7/20 12:38
 * 邮箱：sunjian@cyhz.com
 */
public class ScrollLinear extends LinearLayout {

    private int mdownX,mDownY;

    private Scroller mScroller;

    public ScrollLinear(Context context) {
        this(context,null);
    }

    public ScrollLinear(Context context, AttributeSet attrs) {
        super(context, attrs);
        mScroller = new Scroller(context);
    }

    public ScrollLinear(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs);
    }



    @Override
    public void computeScroll() {

        if (mScroller.computeScrollOffset()) {
            // 让ListView item根据当前的滚动偏移量进行滚动
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            Log.e("sj","mScroller.getCurrX():"+mScroller.getCurrX()+"     mScroller.getCurrY():"+mScroller.getCurrY());
            postInvalidate();
        }

    }


    /**
     * 往右滑动，getScrollX()返回的是左边缘的距离，就是以View左边缘为原点到开始滑动的距离，所以向右边滑动为负值
     */
    private void scrollRight(int deltaX) {
        final int delta = (deltaX+ getScrollX());
        // 调用startScroll方法来设置一些滚动的参数，我们在computeScroll()方法中调用scrollTo来滚动item
        mScroller.startScroll(getScrollX(), 0, -delta, 0,
                Math.abs(delta));
        postInvalidate(); // 刷新itemView
    }


    /**
     * 向左滑动，根据上面我们知道向左滑动为正值
     */
    private void scrollLeft() {
        final int delta = getWidth()/2-getScrollX();
        // 调用startScroll方法来设置一些滚动的参数，我们在computeScroll()方法中调用scrollTo来滚动item
        mScroller.startScroll(getScrollX(), 0,delta, 0,
                Math.abs(delta));
        postInvalidate(); // 刷新itemView
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
            final int action = ev.getAction();
            switch (action) {

                case MotionEvent.ACTION_DOWN:
                    mdownX = (int) ev.getX();
                    break;

                case MotionEvent.ACTION_MOVE:
                    int x = (int) ev.getX();
                    int deltaX = mdownX - x;
                    mdownX = x;

                    // 手指拖动itemView滚动, deltaX大于0向左滚动，小于0向右滚
                    if(deltaX>0&&getScrollX() < getWidth() / 2) {
                            scrollBy(deltaX, 0);
                    }

                    if(deltaX<0&&getScrollX()>0){
                        scrollBy(deltaX, 0);

                    }
                    break;
                case MotionEvent.ACTION_UP:
                    int x1 = (int) ev.getX();
                    int deltaX1 = mdownX - x1;
                    if(getScrollX()<getWidth()/4){
                        scrollRight(deltaX1);
                    }else {
                        Log.e("sj","up scrollX:"+getScrollX());
                        scrollLeft();
                    }

                    break;
            }

            return true;
        }

        //否则直接交给ListView来处理onTouchEvent事件


}
